package com.situ.company.employee.service.impl;

import java.util.List;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.employee.dao.IEmployeeDao;
import com.situ.company.employee.dao.impl.EmployeeDaoImpl;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.IEmployeeService;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.MD5;

public class EmployeeServiceImpl implements IEmployeeService {
	
	private static String pass="123456";
	private IEmployeeDao dao=new EmployeeDaoImpl();
	public String insert(EmployeeModel model) {
		EmployeeModel m0 = new EmployeeModel(model.getCode());
		EmployeeModel mdb = dao.selectModel(m0);
		if (mdb == null) {
			String pass=model.getPass();
			if(FmtEmpty.isEmpty(pass)) {
				pass="123456";
			}
			model.setPass(MD5.encode(pass));
			Integer res = dao.insert(model);
			return res == null ? null : res.toString();
		} else {
			return "repeat";
		}
	}
	public String delete(EmployeeModel model) {
		return dao.delete(model)+"";
	}
	public String update(EmployeeModel model) {
		return dao.updateActive(model)+"";
	}
	public EmployeeModel selectModel(EmployeeModel model) {
		EmployeeModel m0=new EmployeeModel(model.getCode());
		return dao.selectModel(m0);
	}
	public List<EmployeeModel> selectList(EmployeeModel model) {
		String code=model.getCode();
		model.setCode(code==null?"%%":"%"+code+"%");
		String name=model.getName();
		model.setName(name==null?"%%":"%"+name+"%");
		return dao.selectList(model);
	}
	@Override
	public String login(EmployeeModel model) {
		EmployeeModel m0=selectModel(model);
		if(m0==null) {
			return "0";
		}
		String pass=model.getPass();
		pass=MD5.encode(pass);
		return m0.getPass().equals(pass)?"1":"2";
	}

	public Integer selectCount(EmployeeModel model) {
		EmployeeModel e0=new EmployeeModel();
		String code=model.getCode();
		model.setCode(code==null?"%%":"%"+code+"%");
		String name=model.getName();
		model.setName(name==null?"%%":"%"+name+"%");
		return dao.selectCount(e0);
		
	}
	@Override
	public String resetPass(EmployeeModel model) {
		model.setPass(MD5.encode(pass));
		return dao.updateActive(model)+"";
	}
	
	public String updateDept(EmployeeModel model) {
		return dao.updateActive(model)+"";
	}
}
